<!-- views/sales/commission/components/CommissionOperationDialog.vue -->
<template>
  <el-dialog
    v-model="visible"
    :title="dialogTitle"
    width="600px"
    :before-close="handleClose"
  >
    <el-form 
      :model="form" 
      :rules="rules" 
      ref="formRef" 
      label-width="120px"
      @submit.prevent="handleSubmit"
    >
      <el-form-item label="操作类型" prop="action">
        <el-radio-group v-model="form.action">
          <el-radio value="approve">审核通过</el-radio>
          <el-radio value="reject">拒绝提成</el-radio>
          <el-radio value="adjust">调整提成</el-radio>
          <el-radio value="pay">发放提成</el-radio>
        </el-radio-group>
      </el-form-item>
      
      <el-form-item 
        v-if="form.action === 'adjust'" 
        label="调整后提成金额" 
        prop="adjustedAmount"
      >
        <el-input-number 
          v-model="form.adjustedAmount" 
          :min="0" 
          :precision="2" 
          style="width: 100%"
        />
      </el-form-item>
      
      <el-form-item 
        v-if="form.action === 'pay'" 
        label="发放方式" 
        prop="paymentMethod"
      >
        <el-select v-model="form.paymentMethod" placeholder="请选择发放方式" style="width: 100%">
          <el-option label="银行转账" value="bank" />
          <el-option label="现金发放" value="cash" />
          <el-option label="工资合并" value="salary" />
        </el-select>
      </el-form-item>
      
      <el-form-item 
        v-if="form.action === 'pay'" 
        label="发放账户" 
        prop="paymentAccount"
      >
        <el-input v-model="form.paymentAccount" placeholder="请输入发放账户信息" />
      </el-form-item>
      
      <el-form-item label="操作说明" prop="remark">
        <el-input 
          v-model="form.remark" 
          type="textarea" 
          placeholder="请输入操作说明"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="submitting">
        确认操作
      </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, computed, defineProps, defineEmits, watch } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'

interface CommissionReportItem {
  id: number
  salesperson: string
  salespersonId: number
  orderCount: number
  totalAmount: number
  commissionRate: number
  commissionAmount: number
  status: string
  approvalDate: string
  paymentDate: string
  remark: string
}

const props = defineProps<{
  modelValue: boolean
  commissionItem: CommissionReportItem | null
}>()

const emit = defineEmits<{
  (e: 'update:modelValue', value: boolean): void
  (e: 'submit', form: any): void
  (e: 'close'): void
}>()

const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

const formRef = ref<FormInstance>()
const submitting = ref(false)

const form = ref({
  action: 'approve',
  adjustedAmount: 0,
  paymentMethod: 'bank',
  paymentAccount: '',
  remark: ''
})

const dialogTitle = computed(() => {
  if (!props.commissionItem) return '提成操作'
  
  const actionText = {
    approve: '审核提成',
    reject: '拒绝提成',
    adjust: '调整提成',
    pay: '发放提成'
  }
  
  return `${actionText[form.value.action]} - ${props.commissionItem.salesperson}`
})

const rules = ref<FormRules>({
  action: [
    { required: true, message: '请选择操作类型', trigger: 'change' }
  ],
  adjustedAmount: [
    { required: true, message: '请输入调整后提成金额', trigger: 'blur' }
  ],
  paymentMethod: [
    { required: true, message: '请选择发放方式', trigger: 'change' }
  ],
  paymentAccount: [
    { required: true, message: '请输入发放账户信息', trigger: 'blur' }
  ]
})

// 监听操作类型变化，动态调整验证规则
watch(() => form.value.action, (newVal) => {
  if (newVal === 'adjust' && props.commissionItem) {
    form.value.adjustedAmount = props.commissionItem.commissionAmount
  }
}, { immediate: true })

const handleClose = () => {
  visible.value = false
  emit('close')
}

const handleSubmit = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate((valid) => {
    if (valid) {
      submitting.value = true
      try {
        emit('submit', { 
          ...form.value,
          commissionItemId: props.commissionItem?.id
        })
        handleClose()
        ElMessage.success('操作成功')
      } catch (error) {
        ElMessage.error('操作失败')
      } finally {
        submitting.value = false
      }
    }
  })
}
</script>

<style scoped>
.el-form-item {
  margin-bottom: 18px;
}
</style>